package com.nf.db.handlers.map;

import java.sql.ResultSet;
import java.sql.SQLException;

import static com.nf.db.handlers.RowProcessor.DEFAULT_ROWPROCESSOR;

/*
 * 让ResultSetHandler处理结果集，并将结果集转换为map<Object, Object[]>
 * */
public class ColumnMapHandler extends AbstractMapHandler {
    private String keyName;
    private Integer keyIndex;

    public ColumnMapHandler() {
        super();
        this.keyName = null;
        this.keyIndex = 1;
    }

    /*
     * 这里没有参数构造器, 是因为你传进来个参数，无法分辨这是要用作key还是value，所以就没有参数构造器了
     */
    public void setColumnName(String ColumnName) {
        this.columnName = columnName;
    }

    public void setColumnIndex(Integer ColumnIndex) {
        this.columnIndex = columnIndex;
    }

    public void setKeyName(String keyName) {
        this.keyName = keyName;
    }

    public void setKeyIndex(Integer keyIndex) {
        this.keyIndex = keyIndex;
    }

    @Override
    protected Object getValue(ResultSet rs) throws SQLException {
        return DEFAULT_ROWPROCESSOR.toScalar(rs, columnName, columnIndex);
    }
}
